function calculateDistance(lat1, long1, lat2, long2){
  //conversion to radian
  lat1 = (lat1 * 2.0 * Math.PI) / 60.0 / 360.0;  
  long1 = (long1 * 2.0 * Math.PI) / 60.0 / 360.0;
  lat2 = (lat2 * 2.0 * Math.PI) / 60.0 / 360.0;
  long2 = (long2 * 2.0 * Math.PI) / 60.0 / 360.0;
 
  // use to different earth axis length
  var a = 6378137.0;        // Earth Major Axis (WGS84)
  var b = 6356752.3142;     // Minor Axis
  var f = (a-b) / a;        // "Flattening"
  var e = 2.0*f - f*f;      // "Eccentricity"
  
  var beta  = (a / Math.sqrt( 1.0 - e * Math.sin( lat1 ) * Math.sin( lat1 )));
  var cos   = Math.cos( lat1 );
  var x   = beta * cos * Math.cos( long1 );
  var y   = beta * cos * Math.sin( long1 );
  var z   = beta * ( 1 - e ) * Math.sin( lat1 );
 
  beta    = ( a / Math.sqrt( 1.0 -  e * Math.sin( lat2 ) * Math.sin( lat2 )));
  cos     = Math.cos( lat2 );
  x       -= (beta * cos * Math.cos( long2 ));
  y       -= (beta * cos * Math.sin( long2 ));
  z       -= (beta * (1 - e) * Math.sin( lat2 ));
 
  return Math.sqrt( (x*x) + (y*y) + (z*z) );
}

function distance(lat1, lon1, lat2, lon2, unit) {
	var radlat1 = Math.PI * lat1/180
	var radlat2 = Math.PI * lat2/180
	var radlon1 = Math.PI * lon1/180
	var radlon2 = Math.PI * lon2/180
	var theta = lon1-lon2
	var radtheta = Math.PI * theta/180
	var dist = Math.sin(radlat1) * Math.sin(radlat2) + Math.cos(radlat1) * Math.cos(radlat2) * Math.cos(radtheta);
	dist = Math.acos(dist)
	dist = dist * 180/Math.PI
	dist = dist * 60 * 1.1515
	if (unit=="K") { dist = dist * 1.609344 }
	if (unit=="N") { dist = dist * 0.8684 }
	return dist
}  

function distanceTM(x1,y1,x2,y2)
{	//Math.sqrt
	 
	var dist = Math.sqrt(Math.pow(x1-x2,2)+Math.pow(y1-y2,2));
	return dist;
}


var _scale;
var _scaleFlag = false ;
function loadScaleSelect(currScale){
	//scaleSelect
	var sel = $('scaleSelect');
	//sel.addClass('sbox01');
	sel.addEvent('change',  scaleSelect);
	var len =sel.options.length ;
	try { 
			$ES('option', 'scaleSelect').each(function(e, i) {
				e.remove(); 
			}.bind(this));
			new Ajax('pages/map/scale.jsp', {
				method: 'post', 
				onComplete: function (res) {				
					var choices = Json.evaluate(res || false);
					if (!choices || !choices.length) return;
					choices.each( function(choice, i){
						if(parseInt(choice)>0){
							var el = new Element('option').setHTML(makeMoneyType((choice)));
							el.setProperty("value", parseInt(choice));
							if(parseInt(currScale) == parseInt(choice)){
								el.setProperty('selected', 'selected');
								_scaleFlag = true ;
							}	
							sel.adopt(el);
						}else{
							if(_scaleFlag==false){
								var el = new Element('option').setHTML(makeMoneyType((currScale)));
								el.setProperty("value", parseInt(currScale));
								el.setProperty('selected', 'selected');	
								sel.adopt(el);	
							}else{
								_scaleFlag = false ;
							}			
						}
					}, this);
					
					
				}
			}).request();
		
	}catch (err) { 
		alert(err); 
		$('scaleSelect').add(new Option('loading error', null));
	}
}

function makeMoneyType(str) {
	var money = '';
	str = str.substring(0,str.indexOf('.'));
	while ( str.length > 3 ) {
		money += "," + str.substring (str.length -3, str.length); 
		str = str.substring(0, str.length -3); 
	}
	money = str + money;
	return money;
}

function scaleSelect(){
	//scaleSelect
	var sel = $('scaleSelect');
	//sel.addClass('sbox01');
	try { 
		var scale = sel.options[sel.selectedIndex].value;
		var minX , minY , maxX, maxY ;

		minX = (g_WorldX-(g_WorldX-currMinX)*scale/currentScale);
		minY = (g_WorldY-(g_WorldY-currMinY)*scale/currentScale);
		maxX = (g_WorldX+(currMaxX-g_WorldX)*scale/currentScale);
		maxY = (g_WorldY+(currMaxY-g_WorldY)*scale/currentScale);
		getMapImage(minX, minY, maxX, maxY);
		
	}catch (err) { 
		alert(err); 
		$('scaleSelect').add(new Option('loading error', null));
	}
}

function identify(){
	setStartPoint();
	var PosLT = new Array(); // Left Top
	screenToWorld(startX, startY, PosLT);
	//var queryString = { "pointX": PosLT[0], "pointY": PosLT[1] };
	var queryString = { "screenX": startX, "screenY": startY ,"pointX": PosLT[0], "pointY": PosLT[1]};
	new Ajax('getIdentify.do', {
				method: 'post', 
				data : queryString,
				onComplete: function (res) {
				}
			}).request();
}